<script setup lang="tsx">
import { reactive, ref, unref } from 'vue'
import {
  queryListApi,
  addApi,
  delApi,
  updateApi,
  getApi
} from '@{{ api_ts_dir }}/{{ table.table_name }}.ts'
import { useTable } from '@/hooks/web/useTable'
import { useI18n } from '@/hooks/web/useI18n'
import { Table, TableColumn } from '@/components/Table'
import { ElButton, ElSwitch } from 'element-plus'
import { Search } from '@/components/Search'
import { FormSchema } from '@/components/Form'
import { ContentWrap } from '@/components/ContentWrap'
import Write from './components/Write.vue'
import { Dialog } from '@/components/Dialog'

defineOptions({
  name: '{{entity}}'
})

const { t } = useI18n()

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const { pageSize, currentPage } = tableState
    const res = await queryListApi({
      page: unref(currentPage),
      limit: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data || [],
      total: res.count || 0
    }
  },
  fetchDelApi: async (value) => {
    const res = await delApi(value)
    return res.code === 200
  }
})

const { dataList, loading, total, pageSize, currentPage } = tableState
const { getList, delList } = tableMethods

const tableColumns = reactive<TableColumn[]>([
{% for field in table.fields %}
   {
    field: '{{field.name}}',
    label: '{{field.comment}}',
    {% if field.name in ['update_time'] or (field.py_type=='str' and field.max_len and field.max_len>50) %}show: false{% else %}show: true,{% endif %}
  },
{% endfor %}
{% if mode in ['admin','admin_apiquery'] %}
  {
    field: 'action',
    width: '150px',
    label: '操作',
    show: true,
    slots: {
      default: (data: any) => {
        const row = data.row
        const update = ['{{api_permi_base_code}}.update']
        const del = ['{{api_permi_base_code}}.delete']
        return (
          <>
            <ElButton
              type="primary"
              v-hasPermi={update}
              link
              size="small"
              onClick={() => editAction(row)}
            >
              编辑
            </ElButton>
            <ElButton
              type="danger"
              v-hasPermi={del}
              loading={delLoading.value}
              link
              size="small"
              onClick={() => delData(row)}
            >
              删除
            </ElButton>
          </>
        )
      }
    }
  }
{% endif %}
])

const searchSchema = reactive<FormSchema[]>([
{% for field in table.fields %}{% if field.py_type=='str' and field.max_len and field.max_len<=50 %}
  {
    field: '{{field.name}}',
    label: '{{field.comment}}',
    component: 'Input',
    componentProps: {
      clearable: false,
      style: {
        width: '214px'
      }
    }
  }{% if not loop.last %},
  {% endif %} {% endif %}{% endfor %}
])

const searchParams = ref({})
const setSearchParams = (data: any) => {
  currentPage.value = 1
  searchParams.value = data
  getList()
}

const delLoading = ref(false)

const delData = async (row: any) => {
  delLoading.value = true
  await delList(true, [row.id]).finally(() => {
    delLoading.value = false
  })
}

const dialogVisible = ref(false)
const dialogTitle = ref('')

const currentRow = ref()
const actionType = ref('')

const writeRef = ref<ComponentRef<typeof Write>>()

const saveLoading = ref(false)
{% if mode in ['admin','admin_apiquery'] %}
const editAction = async (row: any) => {
  const res = await getApi(row.id)
  if (res) {
    dialogTitle.value = '编辑'
    actionType.value = 'edit'
    currentRow.value = res.data
    dialogVisible.value = true
  }
}

const addAction = () => {
  dialogTitle.value = '新增'
  actionType.value = 'add'
  currentRow.value = undefined
  dialogVisible.value = true
}

const save = async () => {
  const write = unref(writeRef)
  const formData = await write?.submit()
  if (formData) {
    saveLoading.value = true
    try {
      const res = ref({})
      if (actionType.value === 'add') {
        res.value = await addApi(formData)
        if (res.value) {
          dialogVisible.value = false
          getList()
        }
      } else if (actionType.value === 'edit') {
        res.value = await updateApi(formData)
        if (res.value) {
          dialogVisible.value = false
          getList()
        }
      }
    } finally {
      saveLoading.value = false
    }
  }
}
{% endif %}
</script>

<template>
  <ContentWrap>
    <Search :schema="searchSchema" @reset="setSearchParams" @search="setSearchParams" />
    <Table
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      showAction
      :columns="tableColumns"
      default-expand-all
      node-key="id"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total
      }"
      @register="tableRegister"
      @refresh="getList"
    >
      <template #toolbar>
        {% if mode in ['admin','admin_apiquery'] %}
        <ElRow :gutter="10">
          <ElCol :span="1.5">
            <ElButton type="primary" v-hasPermi="['{{ api_permi_base_code }}.create']" @click="addAction"
              >新增</ElButton
            >
          </ElCol>
        </ElRow>
        {% endif %}
      </template>
    </Table>
  </ContentWrap>

  <Dialog v-model="dialogVisible" :title="dialogTitle" :height="650">
    <Write ref="writeRef" :current-row="currentRow" />

    <template #footer>
      <ElButton type="primary" :loading="saveLoading" @click="save">
        {% raw %}{{ t('exampleDemo.save') }} {% endraw %}
      </ElButton>
      <ElButton @click="dialogVisible = false">{% raw %}{{ t('dialogDemo.close') }} {% endraw %}</ElButton>
    </template>
  </Dialog>
</template>
